package cn.yhbk.web.modules.manager.Controller;

import cn.yhbk.web.common.api.CommonPage;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.Entity.GpaChetestCategory;
import cn.yhbk.web.modules.manager.Service.IGpaChetestCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 综测分类表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-09-10
 */
@RestController
@RequestMapping("/gpaChetestCategory")
@Api(tags = "综测分类管理")
public class GpaChetestCategoryController {

    @Autowired
    private IGpaChetestCategoryService categoryService;

    @PostMapping("/")
    @ApiOperation("添加分类")
    public CommonResult addCategory(String name){
        GpaChetestCategory category = new GpaChetestCategory();
        category.setCategoryName(name);
        boolean success = categoryService.save(category);
        if(success)return CommonResult.success("添加成功");
        return CommonResult.failed("添加失败!");
    }

    @GetMapping("/")
    @ApiOperation("获取所有分类")
    public CommonResult getCategory(){
        List<GpaChetestCategory> list = categoryService.list();
        return CommonResult.success(list);
    }

    @ApiOperation("修改分类名称")
    @PutMapping("/{categoryId}")
    public CommonResult updateCategory(@PathVariable Long categoryId,String name){
        GpaChetestCategory category = categoryService.getById(categoryId);
        if(Objects.isNull(category))return CommonResult.failed("该分类不存在!");

        category.setCategoryName(name);
        boolean success = categoryService.updateById(category);
        if(success)return CommonResult.success("修改成功!");

        return CommonResult.failed("修改失败!");
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/{categoryId}")
    public CommonResult deleteCategory(@PathVariable Long categoryId){
        GpaChetestCategory category = categoryService.getById(categoryId);
        if(Objects.isNull(category))return CommonResult.failed("该分类不存在!");
        boolean success = categoryService.removeById(categoryId);
        if(success)return CommonResult.success("删除成功!");
        return CommonResult.failed("删除失败!");
    }

}
